import numpy as np


class RampGenerator:
    def __init__(self, dt: float) -> None:
        super().__init__()
        self.dt = dt
        self.y = 0.0
        self.yDes = 0.0
        self.yOld = 0.0
        self.k = 0.0

    def set_para(self, y_des: float, time: float):
        self.yDes = y_des
        if time < 0.001:
            time = 0.001
        self.k = (self.yDes - self.yOld) / time

    def step(self) -> float:
        self.y = self.yOld + self.k * self.dt
        if np.fabs(self.yDes - self.y) < np.fabs(1.5 * self.k * self.dt):
            self.y = self.yDes
        self.yOld = self.y
        return self.y

    def reset_out(self, y_out: float):
        self.y = y_out
        self.yOld = y_out
